package com.ty.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
//import cn.smart.core.exception.BizException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.ty.config.OssProperties;
import com.ty.dto.FileInfo;
import com.ty.enums.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class OssService {
    @Autowired
    private OssProperties ossProperties;
    OSS ossClient;
    //1.bean 定义  2.实例化  3.属性注入  4. 初始化  5. 使用  6.销毁
    @PostConstruct
    public  void  init(){
        ossClient= new OSSClientBuilder().build(ossProperties.getEndpoint(),ossProperties.getAccessKeyId(),ossProperties.getAccessKeySecret());
    }

    private static List<String> img_ext  = CollUtil.newArrayList("jpg","png","gif","bmp","jpeg","webp","tif","psd","pdf","dwg","dxf","ico");

    public  String  uploadFile(String fileName,InputStream inputStream){
        String suffix = FileUtil.getSuffix(fileName);
        Optional<FileType> first = Arrays.stream(FileType.values()).filter(type -> type.getExts().contains(suffix)).findFirst();
        if(!first.isPresent()){
            BizException.throwException(405,"不支持的文件类型");
        }
        String folder = first.get().getFolder();

        fileName = folder + fileName;
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucketName(), fileName, inputStream);
        // 创建PutObject请求。
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        // 设置预签名URL过期时间，单位为毫秒。本示例以设置过期时间为1小时为例。
        Date expiration = new Date(new Date().getTime() + 50*365*24*3600 * 1000L);
        // 生成以GET方法访问的预签名URL。本示例没有额外请求头，其他人可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(ossProperties.getBucketName(), fileName, expiration);
        return  url.toString();
    }

    public  List<FileInfo>   select(Integer code){
        FileType fileType = FileType.getByCode(code);
        String folder = fileType.getFolder();
        // 列举文件。如果不设置keyPrefix，则列举存储空间下的所有文件。如果设置keyPrefix，则列举包含指定前缀的文件。
        ObjectListing objectListing = ossClient.listObjects(ossProperties.getBucketName(), folder);
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        List<OSSObjectSummary> files = sums.stream().filter(s -> s.getSize() > 0).collect(Collectors.toList());
        List<FileInfo> fileInfos = files.stream().map(file -> {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setName(file.getKey());
            Date expiration = new Date(new Date().getTime() + 50*365*24*3600 * 1000L);
            // 生成以GET方法访问的预签名URL。本示例没有额外请求头，其他人可以直接通过浏览器访问相关内容。
            URL url = ossClient.generatePresignedUrl(ossProperties.getBucketName(), file.getKey(), expiration);
            fileInfo.setUrl(url.toString());
            return fileInfo;
        }).collect(Collectors.toList());
        return fileInfos;
    }

    private static class BizException {
        public static void throwException(int i, String 不支持的文件类型) {
        }
    }
}
